﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EndlessGalaxy
{
    public partial class facilityConfig : Form
    {
        Facility fac;
        public facilityConfig(Facility target)
        {
            InitializeComponent();
            if (target.shipbuilder)
            {
                //Do ship builder things here
            }
            else
            {
                if (target.miner)
                {
                    comboBox1.Items.Add("Quarry");
                    foreach (SuperCargo stuff in Globals.materials.Values)
                    {
                        if (Cargo.HasContents(stuff.name, Globals.knownsystems[0].planets[target.location].composition))
                        {
                            comboBox1.Items.Add(stuff.name);
                        }
                    }
                    button1.Enabled = false;
                }
                else
                {
                    foreach (SuperCargo stuff in Globals.materials.Values)
                    {
                        if (!stuff.elemental)
                        {
                            comboBox1.Items.Add(stuff.name);
                        }
                    }
                }
            }
            fac = target;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            switch (button1.Text)
            {
                case "Separate":
                    button1.Text = "Create";
                    break;
                default:
                    button1.Text = "Separate";
                    break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Dictionary<string, double> stuff;
            if (comboBox1.SelectedIndex > -1)
            {
                if (fac.miner)
                {
                    if (Convert.ToString(comboBox1.Items[comboBox1.SelectedIndex]) == "Quarry")
                    {
                        fac.input = null;
                        fac.output = null;
                    }
                    else
                    {
                        fac.input = new List<Cargo>() { new Cargo(Convert.ToString(comboBox1.Items[comboBox1.SelectedIndex]),
                        fac.level, Cargo.Phases.other) };
                        fac.output = new List<Cargo>() { new Cargo(Convert.ToString(comboBox1.Items[comboBox1.SelectedIndex]),
                        fac.level, Cargo.Phases.other) };
                    }
                }
                else
                {
                    switch (button1.Text)
                    {
                        case "Separate":
                            fac.input = new List<Cargo>() { new Cargo(Convert.ToString(comboBox1.Items[comboBox1.SelectedIndex]),
                        fac.level, Cargo.Phases.other) };
                            fac.output = new List<Cargo>();
                            stuff = Globals.materials[Convert.ToString(comboBox1.Items[comboBox1.SelectedIndex])].contents();
                            foreach (string k in stuff.Keys)
                            {
                                fac.output.Add(new Cargo(k, stuff[k], Globals.materials[k].phase));
                            }
                            break;
                        default:
                            fac.output = new List<Cargo>() { new Cargo(Convert.ToString(comboBox1.Items[comboBox1.SelectedIndex]),
                        fac.level, Globals.materials[Convert.ToString(comboBox1.Items[comboBox1.SelectedIndex])].phase) };
                            fac.input = new List<Cargo>();
                            stuff = Globals.materials[Convert.ToString(comboBox1.Items[comboBox1.SelectedIndex])].contents();
                            foreach (string k in stuff.Keys)
                            {
                                fac.input.Add(new Cargo(k, stuff[k], Cargo.Phases.solid));
                            }
                            break;
                    }
                }
            }
            this.Close();
        }

    }
}
